package com.fyzk.activity.lottery.shuzicai;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.down_numhistory.NumHistoryPrizeView;
import com.fyzk.activity.lottery.basketball.JCLQSelectPlayMethodPop;
import com.fyzk.activity.lottery.shuzicai.TouZhuHelper.BallClickListener;
import com.fyzk.activity.lottery.shuzicai.TouZhuHelper.BallItemClickLitener;
import com.fyzk.activity.myview.BallPopFrameLayout;
import com.fyzk.activity.myview.BallScrollView;
import com.fyzk.activity.myview.DrawBallFrameLayout;
import com.fyzk.activity.myview.WeiZhi;
import com.fyzk.activity.trend.DLTTrendActivity;
import com.fyzk.bean.PrizeHistoryItem;
import com.fyzk.betzhushu.ZhuShuFactory;
import com.tyjc.lottery.R;
import com.fyzk.random.RandomFactory;
import com.fyzk.utils.HelpUrlConstants;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.TopTitleView;
import com.fyzk.view.ViewCallBack;

import java.util.ArrayList;
import java.util.List;


public class DLTActivity extends LotteryBaseActivity implements
		BallClickListener, BallItemClickLitener,  JCLQSelectPlayMethodPop.OnWFCheckChangedListener {

	private BallPopFrameLayout moveQiu;

	private TopTitleView topView;

	private DrawBallFrameLayout ball_linear;
	private BallScrollView ball_scroll;

	SelectBallView ballView_firstLine;
	SelectBallView ballView_twoline;
	SelectBallView ballView_dan_lan;
	SelectBallView ballView_threeline;

	private PopSSQSelectPlayMethod popSSQSelectPlayMethod;

	List<BallInfo> balls_firstLine = new ArrayList<BallInfo>();
	List<BallInfo> balls_twoLine = new ArrayList<BallInfo>();
	List<BallInfo> balls_dan_lan = new ArrayList<BallInfo>();
	List<BallInfo> balls_threeLine = new ArrayList<BallInfo>();

	private final int MAX_HONG = 18;
	private final int MAX_QIANTUOMA = 24;
	private final int MIN_QIANTUOMA = 2;

	private TextView bottom_zhushu;

	private TextView bottom_money;

	private LinearLayout ll_history;

	private int wcodeId = LotteryIDUtils.DLT_LOTTERY_ID_INT;
	// private final String[] pop_titles = {"直选","胆拖","走势图","介绍"};
	private final String[] pop_titles = { "直选", "胆拖", "介绍" };
	// private final int[] pop_img = {R.drawable.ssq_dianxuan,
	// R.drawable.ssq_dianxuan, R.drawable.ssq_zoushitu,
	// R.drawable.icon_jieshao};

	private String[] popup_items = {  "直选", "胆拖" };

	private LinearLayout dan_hong_ll;
	private LinearLayout dan_lan_ll;
	private TextView ballTitle_hong;
	private TextView ballTitle_lan;
	private TextView hong_tishi;
	private TextView lan_tishi;
	private LinearLayout yao_ll;

	protected final int hong_count = 35;
	protected final int lan_count = 12;
	protected final int dan_hong_max = 4;
	protected final int dan_lan_max = 1;

	private final int ZHIXUAN = 0;
	private final int DANTUO = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dj_activity_dlt);
		initBallView();
		ViewCallBack.getInstance().AttachCountDownObj(this);
		wcodeId = LotteryIDUtils.DLT_LOTTERY_ID_INT;
//		popupClickItem(popupId);
//		ballClick();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public TopTitleView getTopView() {
		return topView;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == 0 && resultCode == 1) {

			List<BallInfo> b_twoLine = (List<BallInfo>) data
					.getSerializableExtra(IntentParamUtils.ZST_HONG);
			List<BallInfo> b_threeLine = (List<BallInfo>) data
					.getSerializableExtra(IntentParamUtils.ZST_LAN);

			for (int i = 0; i < b_twoLine.size(); i++) {
				balls_twoLine.get(i).setSelected(b_twoLine.get(i).isSelected());
			}
			for (int i = 0; i < b_threeLine.size(); i++) {
				balls_threeLine.get(i).setSelected(
						b_threeLine.get(i).isSelected());
			}
			ballView_twoline.invalidate();
			ballView_threeline.invalidate();

			ballClick();
		} else {
			ballView_twoline.invalidate();
			ballView_threeline.invalidate();
			ballClick();
		}
	}

	private void initBallView() {

		TouZhuHelper.getInstance().setBallClickListener(this);
		TouZhuHelper.getInstance().setBallItemClickLitener(this);

		topView = (TopTitleView) findViewById(R.id.topTitle);
		topView.setCenterText(R.string.dlt);
		topView.setOnTitleClickListener(this);
		topView.setJianTouVisible(View.VISIBLE);

		popSSQSelectPlayMethod = new PopSSQSelectPlayMethod(this,
				AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
		popSSQSelectPlayMethod.setOnWFCheckChangedListener(this);
		
		moveQiu = (BallPopFrameLayout) findViewById(R.id.pl5_fl_moveqiu);
		ball_linear = (DrawBallFrameLayout) findViewById(R.id.pl5_drawball_framelayout);
		ball_scroll = (BallScrollView) findViewById(R.id.pl5_ballscroll);
		ball_linear.setScr(ball_scroll);
		ball_linear.setFl_moveqiu(moveQiu);
		ball_linear.setUpOrDownListener(this);
		ballView_firstLine = (SelectBallView) findViewById(R.id.firstLine);
		ballView_twoline = (SelectBallView) findViewById(R.id.twoLine);
		ballView_dan_lan = (SelectBallView) findViewById(R.id.dan_lan);
		ballView_threeline = (SelectBallView) findViewById(R.id.thressLine);
		bottom_zhushu = (TextView) findViewById(R.id.pl5_bottom_tv_zhushu);
		bottom_money = (TextView) findViewById(R.id.pl5_bottom_tv_money);

		dan_hong_ll = (LinearLayout) findViewById(R.id.dlt_dan_ll);
		dan_lan_ll = (LinearLayout) findViewById(R.id.dlt_dan_lan_ll);
		ballTitle_hong = (TextView) findViewById(R.id.dlt_balltitle_h);
		ballTitle_lan = (TextView) findViewById(R.id.dlt_balltitle_l);
		hong_tishi = (TextView) findViewById(R.id.dlt_hong_tishi);
		lan_tishi = (TextView) findViewById(R.id.dlt_lan_tishi);
		yao_ll = (LinearLayout) findViewById(R.id.dlt_yao_ll);
		yao_ll.setOnClickListener(this);

		// 新加
		ll_history = (LinearLayout) findViewById(R.id.dlt_ball_history);

		initArrayInfos();

		ballView_twoline.setSelect_max(MAX_HONG);
		ballView_firstLine.setSelect_max(dan_hong_max);// 胆拖红球最多可选5个球
		ballView_dan_lan.setSelect_max(dan_lan_max);// 胆拖蓝球最多可选1个球

		// 设置关联选球SelectView
		ballView_firstLine.setGuanLian_view(ballView_twoline);
		ballView_twoline.setGuanLian_view(ballView_firstLine);

		ballView_dan_lan.setGuanLian_view(ballView_threeline);
		ballView_threeline.setGuanLian_view(ballView_dan_lan);

		initBottomView();
	}

	@Override
	protected void initArrayInfos() {
		if (arr_infos.size() == 4) {
			balls_firstLine = arr_infos.get(0);
			balls_twoLine = arr_infos.get(1);
			balls_dan_lan = arr_infos.get(2);
			balls_threeLine = arr_infos.get(3);
		} else {//初始化小球列表
			arr_infos.clear();
			initBallList(balls_firstLine, hong_count, 1,true);
			initBallList(balls_twoLine, hong_count, 1,true);
			initBallList(balls_dan_lan, lan_count, 1,true);
			initBallList(balls_threeLine, lan_count, 1,true);
			arr_infos.add(balls_firstLine);
			arr_infos.add(balls_twoLine);
			arr_infos.add(balls_dan_lan);
			arr_infos.add(balls_threeLine);
		}
		ballView_firstLine.setBallInfoList(balls_firstLine);
		ballView_twoline.setBallInfoList(balls_twoLine);
		ballView_dan_lan.setBallInfoList(balls_dan_lan);
		ballView_threeline.setBallInfoList(balls_threeLine);
	}

	@Override
	public void ballClick(WeiZhi info) {
		// TODO Auto-generated method stub
		WeiZhi w = info;
		// TODO Auto-generated method stub
		int left = w.getLeft() - 5;
		int right = w.getRight() + 5;
		int top = (int) ((ball_linear.getY1() - w.getPointY() + w.getTop())
				- (w.getBottom() - w.getTop()) - 10);
		int bottom = (int) (ball_linear.getY1() - w.getPointY() + w.getTop());
		Rect rect = new Rect(left, top, right, bottom);
		moveQiu.setRect(rect, w.getNum(), w.getType(), w.isShowMoveBall);
	}

	@Override
	public void ballClick() {
		// TODO Auto-generated method stub

		long zhushu = getZhushu();
		long money = zhushu * 2;
		bottom_zhushu.setText(zhushu + "注 ");
		bottom_money.setText("共" + money + "米");

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()){
			case R.id.dlt_yao_ll:
				randomBallNum();
				break;
		}
	}

	private long getZhushu() {
		return ZhuShuFactory.getZhuShu(wcodeId).betZhuShu(arr_infos);
	}

	@Override
	public boolean touZhu() {
		// TODO Auto-generated method stub
		long zhuShu = getZhushu();
		if (zhuShu > 0) {
			if (wcodeId != LotteryIDUtils.DLT_LOTTERY_TOWED_ID_INT) {
				wcodeId = zhuShu > 1 ? LotteryIDUtils.DLT_LOTTERY_COMMON_FU_ID_INT
						: LotteryIDUtils.DLT_LOTTERY_COMMON_DAN_ID_INT;
			}
			int lotteryId = wcodeId;
			touZhu1(lotteryId, zhuShu);

			return true;
		} else {
			return false;
		}
	}

	@Override
	public void onRightClick() {
		// TODO Auto-generated method stub
		toHelp();
	}

	@Override
	public int getLotteryId() {
		// TODO Auto-generated method stub
		return LotteryIDUtils.DLT_LOTTERY_ID_INT;
	}

	@Override
	public void randomBallNum() {
		// TODO Auto-generated method stub
		int two[] = RandomFactory.getRandom(getLotteryId()).randomDanHang(5);
		int thire[] = RandomFactory.getRandom(getLotteryId()).randomLan(2);

		initBallList(balls_twoLine, hong_count, 1,true);
		initBallList(balls_threeLine, lan_count, 1,true);

		for (int i = 0; i < two.length; i++) {
			balls_twoLine.get(two[i]).setSelected(true);
		}
		for (int i = 0; i < thire.length; i++) {
			balls_threeLine.get(thire[i]).setSelected(true);
		}

		ballView_twoline.invalidate();
		ballView_threeline.invalidate();

		ballClick();
	}

	@Override
	protected void addHistoryView(ArrayList<PrizeHistoryItem> historyItems,
			boolean isWaitting) {
		ll_history.removeAllViews();
		ll_history.addView(new NumHistoryPrizeView(getContext(), historyItems)
				.getView());
	}

	@Override
	protected void popupClickItem(int arg2) {
		popupId = arg2;
		popSSQSelectPlayMethod.setCheckItem(arg2);
		switch (arg2) {
		case 0:
			onWFCheckChange(R.id.cb_selectplaymethod_normal_ssq, false);
			break;
		case 1:
			onWFCheckChange(R.id.cb_selectplaymethod_array_ssq, false);
			break;
		}
	}

	@Override
	public void clear() {
		bottom_zhushu.setText(0 + "注 ");
		bottom_money.setText("共" + 0 + "米");

		initBallList(balls_firstLine, hong_count, 1,true);
		initBallList(balls_twoLine, hong_count, 1,true);
		initBallList(balls_dan_lan, lan_count, 1,true);
		initBallList(balls_threeLine, lan_count, 1,true);

		ballView_firstLine.setSelect_count(0);
		ballView_twoline.setSelect_count(0);
		ballView_dan_lan.setSelect_count(0);
		ballView_threeline.setSelect_count(0);

		ballView_firstLine.invalidate();
		ballView_twoline.invalidate();
		ballView_dan_lan.invalidate();
		ballView_threeline.invalidate();
	}

	@Override
	public String getHelpUrl() {
		// TODO Auto-generated method stub
		return HelpUrlConstants.Help_DLT;
	}

	@Override
	public String getHelpTitle() {
		// TODO Auto-generated method stub
		return getString(R.string.help_dlt);
	}


	@Override
	public void onCenterClick() {
		// TODO Auto-generated method stub
		popSSQSelectPlayMethod.showAsDropDown(topView);
	}


	@Override
	public void onWFCheckChange(int id, boolean isChecked) {
		clear();
		switch (id) {
			case R.id.cb_selectplaymethod_normal_ssq:
				yao_ll.setVisibility(View.VISIBLE);
				isYao = true;
				topView.setCenterText(R.string.dlt);
				dan_hong_ll.setVisibility(View.GONE);
				dan_lan_ll.setVisibility(View.GONE);
				ballTitle_hong.setText(getString(R.string.dlt_hong));
				ballTitle_lan.setText(getString(R.string.dlt_lan));
				hong_tishi.setText(getString(R.string.dlt_tishi));
				lan_tishi.setText("");

				wcodeId = LotteryIDUtils.DLT_LOTTERY_ID_INT;

				ballView_twoline.setSelect_max(MAX_HONG);
				popupId = 0;
				break;
			case R.id.cb_selectplaymethod_array_ssq:
				yao_ll.setVisibility(View.INVISIBLE);
				isYao = false;
				topView.setCenterText(R.string.dlt_dantuo);
				dan_hong_ll.setVisibility(View.VISIBLE);
				dan_lan_ll.setVisibility(View.VISIBLE);
				ballTitle_hong.setText(getString(R.string.dlt_tuo_qian));
				ballTitle_lan.setText(getString(R.string.dlt_tuo_hou));
				hong_tishi.setText(getString(R.string.dlt_tuo_tishi));
				lan_tishi.setText(getString(R.string.dlt_tuo_hou_tishi));

				wcodeId = LotteryIDUtils.DLT_LOTTERY_TOWED_ID_INT;
				ballView_twoline.setSelect_max(MAX_QIANTUOMA);
				popupId = 1;
				break;
		}
		popSSQSelectPlayMethod.dismiss();

	}

	@Override
	protected void addMissValue() {
		super.addMissValue();
		if (missInfo != null) {
			ballView_firstLine.setMissValues(missInfo.missValue("2001MV_X").get(0));
			ballView_twoline.setMissValues(missInfo.missValue("2001MV_X").get(0));
			ballView_dan_lan.setMissValues(missInfo.missValue("2001MV_X").get(1));
			ballView_threeline.setMissValues(missInfo.missValue("2001MV_X").get(1));

		}

	}

	@Override
	public Intent getTrendIntent() {
		Intent intent = new Intent(getContext(), DLTTrendActivity.class);
		intent.putExtra(IntentParamUtils.LOTTERYID, getLotteryId());
		return intent;
	}
}
